package com.tianjin.weight.controller.pc;

import com.tianjin.weight.aop.annotation.LogAnnotation;
import com.tianjin.weight.entity.Ao;
import com.tianjin.weight.entity.Page;
import com.tianjin.weight.entity.SysUser;
import com.tianjin.weight.model.pc.UserWithPing;
import com.tianjin.weight.service.base.UserService;
import com.tianjin.weight.service.ping.UserWithPingService;
import com.tianjin.weight.utils.DataResult;
import com.tianjin.weight.utils.JwtTokenUtil;
import com.tianjin.weight.utils.MapperBeanUtils;
import com.tianjin.weight.vo.req.UserAddReqVO;
import com.tianjin.weight.vo.req.UserPageReqVO;
import com.tianjin.weight.vo.req.UserUpdateReqVO;
import com.tianjin.weight.vo.resp.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@RestController
@Api(tags = "组织模块-用户管理")
@RequestMapping("/pc")
public class PcPingUserController {
    //=======================================平台===========================================
    @GetMapping("/ping/user")
    @ApiOperation(value = "分页获取用户列表接口")
    @RequiresPermissions("pc:ping:user:list")
    @LogAnnotation(title = "pc-平台用户管理",action = "分页获取用户列表")
    public DataResult<PageVO<SysUser>> getList(UserPageReqVO vo, HttpServletRequest request) throws Exception {
        String accessToken=request.getHeader("authorization");
        Long userId= JwtTokenUtil.getUserId(accessToken);
        DataResult<PageVO<SysUser>> result= DataResult.success();
        UserService userService = new UserWithPingService(
                new UserWithPing(
                        vo,
                        null,
                        null,
                        Page.builder().pageNum(vo.getPageNum()).pageSize(vo.getPageSize()).build(),
                        null
                ),
                null,
                null,
                null,
                null,
                null
        );
        result.setData(userService.getList());
//        result.setData(userService.pageInfoPing(vo,userId));
        return result;
    }

    @GetMapping("/ping/user/{id}")
    @ApiOperation(value = "分页获取用户列表接口")
    @RequiresPermissions("pc:ping:user:list")
    @LogAnnotation(title = "pc-平台用户管理",action = "分页获取用户列表")
    public DataResult<SysUser> getOne(@PathVariable("id") Long id, HttpServletRequest request) throws Exception {
        /**
         * 通过access_token拿userId
         */
        String accessToken=request.getHeader("authorization");
        Long userId= JwtTokenUtil.getUserId(accessToken);
        DataResult<SysUser> result=DataResult.success();
        UserService userService = new UserWithPingService(
                new UserWithPing(
                        null,
                        null,
                        null,
                        null,
                        Ao.builder().id(id).build()
                ),
                null,
                null,
                null,
                null,
                null
        );
        result.setData((SysUser) userService.getOne());
//        result.setData(userService.pageInfoPing(id,userId));
        return result;
    }

    @PostMapping("/ping/user")
    @ApiOperation(value = "新增用户接口")
    @RequiresPermissions("pc:ping:user:insert")
    @LogAnnotation(title = "pc-平台用户管理",action = "新增用户")
    public DataResult add(@RequestBody @Valid UserAddReqVO vo, HttpServletRequest request) throws Exception {
        String accessToken=request.getHeader("authorization");
        Long userId= JwtTokenUtil.getUserId(accessToken);
        UserService userService = new UserWithPingService(
                new UserWithPing(
                        (SysUser) vo,
                        null,
                        null,
                        null,
                        Ao.builder().classifyId(3L).build()
                ),
                null,
                null,
                null,
                null,
                null
        );
        SysUser sysUser = MapperBeanUtils.getSysUserMapper().getUserInfoByPhone(vo.getPhone());
        if(sysUser != null) {
            DataResult dataResult = new DataResult();
            dataResult.setCode(400);
            dataResult.setMsg("新增失败，已存在相同的手机号");
            return dataResult;
        }else {
            userService.add();
            return DataResult.success();
        }
    }

    @PutMapping("/ping/user")
    @ApiOperation(value = "修改用户接口")
    @RequiresPermissions("pc:ping:user:update")
    @LogAnnotation(title = "pc-平台用户管理",action = "修改用户接口")
    public DataResult update(@RequestBody @Valid UserUpdateReqVO vo) throws Exception {
        UserService userService = new UserWithPingService(
                new UserWithPing(
                        (SysUser) vo,
                        null,
                        null,
                        null,
                        Ao.builder().id(vo.getId()).build()
                ),
                null,
                null,
                null,
                null,
                Ao.builder().id(vo.getId()).status(vo.getStatus()).build()
        );
        userService.update();
//        userService.updateUserPing(vo);
        return DataResult.success();
    }

}
